// pkg_wo/pages/tuiKuan/tuiKuan.js
const app = getApp();
import request from '../../../utils/http.js'
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderV:1,//新旧订单类型：1，20230525的订单版本，老的版本，2,20231109的订单版本，新版本

    personId: 0,
    orderFormId: 0,
    pric: 0, //退款金额
    tuiKuanType: 1, //1 取消订单  2 我要退款(无需退货) 3 我要退货退款
    tuiKuanTypeStr: '取消订单',
    visible: false,
    pickerType: 1, //弹框类型 1 类型选项 2原因选项
    shenqingStr: '', //申请原因
    shuoMingStr: '', //详情说明
    phone: '', //联系电话
    imageUrl1: '',
    imageUrl2: '',
    imageUrl3: '',
    imageFlage: false, //最多3三图片
    isReadyFlag: false, //必填项是否都已填写

    actionUrl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options.type)
    this.setData({
      personId: wx.getStorageSync(app.globalData.PERSON_ID),
      orderFormId: options.orderFormId,
      tuiKuanType: options.type,
      tuiKuanTypeStr: options.type == 1 ? '取消订单' : '我要退款退货',
      phone: wx.getStorageSync(app.globalData.PHONENUMBER),
      pric: options.pric,
      orderV:options.orderV,
      actionUrl:options.orderV==1?'dingDan':'OrderForm20231109'
    })
  },

  //修改申请类型，tuiKuanType=1不可修改 2、3可以选
  changeType: function (e) {
    var index = e.currentTarget.dataset.index
    if (index == 2) {
      this.setData({
        tuiKuanType: 2,
        tuiKuanTypeStr: '我要退款(无需退货)',
        visible: false
      })
    } else {
      this.setData({
        tuiKuanType: 3,
        tuiKuanTypeStr: '我要退款退货',
        visible: false
      })
    }
  },


  //修改申请类型，tuiKuanType=1不可修改 2、3可以选
  changeShuoming: function (e) {
    var index = e.currentTarget.dataset.index
    var shenqingStr = '';
    switch (index) {
      case '1':
        shenqingStr = '多拍、错拍、不想要';
        break;
      case '2':
        shenqingStr = '协商一致退款';
        break;
      case '3':
        shenqingStr = '不喜欢、效果不好';
        break;
      case '4':
        shenqingStr = '材质与商品描述不符';
        break;
      case '5':
        shenqingStr = '大小尺寸与商品描述不符';
        break;
      case '6':
        shenqingStr = '质量问题';
        break;
      case '7':
        shenqingStr = '收到商品少件、破损或污渍';
        break;
      case '8':
        shenqingStr = '商家发错货';
        break;
      case '9':
        shenqingStr = '冒牌产品';
        break;
      default:
        shenqingStr = '其他';
    }
    this.setData({
      shenqingStr: shenqingStr,
      visible: false
    })
    this.isReadyCheck();
  },

  //申请说明输入项
  bindTextAreaBlur: function (e) {
    //console.log(e.detail.value)
    this.setData({
      shuoMingStr: e.detail.value
    })
    this.isReadyCheck();
  },

  //联系电话输入项
  bindTextPhone: function (e) {
    //console.log(e.detail.value)
    this.setData({
      phone: e.detail.value
    })
    this.isReadyCheck();
  },

  //判断是否已输完
  isReadyCheck: function () {
    if (this.data.shuoMingStr != '' && this.data.phone != '' && this.data.tuiKuanType != '' && this.data.shenqingStr != '') {
      this.setData({
        isReadyFlag: true,
      })
    } else {
      this.setData({
        isReadyFlag: false,
      })
    }
  },

  //提交退款申请
  tuiKuanAction:function(){
    if(this.data.isReadyFlag){
      var that=this
      var url=that.data.actionUrl+'/quXiaoOrTuiHuo'

      let data={
        orderFormId:that.data.orderFormId,
        leiXing:that.data.tuiKuanType,
        leiXingShuoMing:that.data.tuiKuanTypeStr,
        yuanYin:that.data.shenqingStr,
        shenQingShuoMing:that.data.shuoMingStr,
        imageUrl1:that.data.imageUrl1,
        imageUrl2:that.data.imageUrl2,
        imageUrl3:that.data.imageUrl3,
        lianXiDianHua:that.data.phone
      }
      request.get(url,data,res=>{
        
      console.log("退款申请:"+JSON.stringify(res.data))
        if(res.data.status==1){
      
          wx.redirectTo({
            url: '/pkg_wo/pages/allOrders/allOrders?index=5',
            })

        }else{
          wx.showToast({
            title: "申请失败！", // 提示的内容
            icon: "none", // 图标，默认success
            duration: 3000, // 提示的延迟时间，默认1500
            mask: false, // 是否显示透明蒙层，防止触摸穿透
          })
        }

      })
    }else{
      wx.showToast({
        title: "请完整填写申请信息！", // 提示的内容
        icon: "none", // 图标，默认success
        duration: 3000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    }
  },


  //删除图片
  delImg: function (e) {
    var index = e.currentTarget.dataset.index

    if (index == 1) {
      this.setData({
        imageUrl1: '',
        imageFlage: false
      })
    } else if (index == 2) {
      this.setData({
        imageUrl2: '',
        imageFlage: false
      })
    } else {
      this.setData({
        imageUrl3: '',
        imageFlage: false
      })
    }

  },

  //上传图片
  bindKaotiImgFront: function () {
    var that = this
    wx.showActionSheet({
      itemList: ['拍照', '从相册选择'],
      success: function (res) {
        //拍照
        if (res.tapIndex == 0) {
          that.openCamera()
        } else if (res.tapIndex == 1) {
          //从相册选择
          that.openPhoto()
        }
      }
    })
    // }
  },

  /**
   * 打开照相机
   */
  openCamera: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["camera"],
      success: function (res) {
        var filePath = res.tempFilePaths[0]
        that.uploadToQiniu(filePath)
      },
    })
  },

  /**
   * 从相册选择
   */
  openPhoto: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["album"],
      success: function (res) {
        var filePath = res.tempFilePaths[0]
        that.uploadToQiniu(filePath)
      },
    })
  },



  /**
   * 上传图片到七牛
   */
  uploadToQiniu: function (filePath) {
    wx.showLoading({
      title: '上传中...',
      mask: true
    })
    var that = this
    wx.request({
      url: app.globalData.base_url + 'resource/getQiNiuToken',
      success: function (res) {

        // console.log("res.data.uptoken:" + res.data.uptoken),
        // console.log("res.data.key:" + res.data.key)
        wx.uploadFile({
          url: 'https://up-z1.qbox.me', //七牛云存储华东地区
          filePath: filePath,
          name: 'file',
          formData: {
            token: res.data.uptoken,
            key: res.data.key
          },
          success: function (res) {
           // console.log("imageUrl:res.data:" + res.data)
            var dataObject = JSON.parse(res.data);
            var imageUrl = app.globalData.base_qiniu_url + dataObject.key;
          //  console.log("imageUrl:" + imageUrl)

            if (that.data.imageUrl1 == '') {
              that.setData({
                imageUrl1: imageUrl
              })
            } else if (that.data.imageUrl2 == '') {
              that.setData({
                imageUrl2: imageUrl
              })
            } else {
              that.setData({
                imageUrl3: imageUrl
              })
            }

            if (that.data.imageUrl1 != '' && that.data.imageUrl2 != '' && that.data.imageUrl3 != '') {
              that.setData({
                imageFlage: true
              })
            }

          }
        })
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },


  //点击显示弹框
  pickAddress(e) {
    var index = e.currentTarget.dataset.index;
   // console.log(index)
    this.setData({
      visible: true,
      pickerType: index,
      pickerTxt: index == 1 ? '申请类型' : "申请原因"
    })
  },

  // 关闭弹框
  citySure(e) {
    this.setData({
      visible: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})